package org.example.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 返回数据格式定义
 */
@Data
@Accessors(chain = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResultData<T> {

    /**
     * 返回信息
     */
    private String message;//返回信息

    /**
     * 返回状态
     */
    private String code;//返回状态

    /**
     * 返回对象
     */
    private T obj;//返回对象

    /**
     * 返回对象集合(若需要返回多个对象, 可使用此属性)
     */
    private T[] objs;//返回对象集合

    public ResultData() {
    }

    public ResultData<T> ok() {
        this.message = ResultStatus.SUCCESS.getIns();
        this.code = ResultStatus.SUCCESS.getStatus();
        return this;
    }

    public ResultData<T> ok(T t) {
        this.message = ResultStatus.SUCCESS.getIns();
        this.code = ResultStatus.SUCCESS.getStatus();
        this.obj = t;
        return this;
    }

    public ResultData<T> failed() {
        this.message = ResultStatus.FAILED.getIns();
        this.code = ResultStatus.FAILED.getStatus();
        return this;
    }

    public ResultData<T> failed(T t) {
        this.message = ResultStatus.FAILED.getIns();
        this.code = ResultStatus.FAILED.getStatus();
        this.obj = t;
        return this;
    }


    @SafeVarargs
    public ResultData(String message, String code, T... objs) {
        this.message = message;
        this.code = code;
        this.objs = objs;
    }

    public ResultData(String message, String code, T obj) {
        this.message = message;
        this.code = code;
        this.obj = obj;
    }

    public ResultData(String message, String code) {
        this.message = message;
        this.code = code;
    }

    @SafeVarargs
    public final ResultData<T> setObjs(T... objs) {
        if (objs != null) {
            this.objs = objs;
        }
        return this;
    }
}
